import SDK from '../../rn/NIM_Web_SDK_rn_v8.8.1.js';
export const Base64ToBlob = SDK.NIM.blob.fromDataURL;
export default async function creatChatroom(account, onChatroomMsgs) {
  let tokens = ['', '', '78d8bbff69b51f200cfe26b87d94e831', 'c03f502f5bf6b35472f2f81f3b972480', '586825f9ecc4296033ba747a1487db05'];
  let token = tokens[account];
  let appKey = '8e05ff6397a0b386db021c58d6874085';
  let chatroomId = '459306108';
  const getChatroom = async () => {
    return await new Promise(resove => {
      let nim = SDK.NIM.getInstance({
        debug: true,
        appKey,
        account,
        token,
        db: true,
        onconnect: function onConnect() {
          nim.getChatroomAddress({
            chatroomId,
            done: function (error, obj) {
              resove(obj.address);
            },
          });
        },
        onwillreconnect: function onWillReconnect(obj) {
        },
        ondisconnect: function onDisconnect(error) {
        },
        onerror: function onError(error) {
        },
      });
    });
  };
  const initChatRoom = async () => {
    let chatroomAddresses = await getChatroom();
    return await new Promise(resove => {
      let myChatroom = SDK.Chatroom.getInstance({
        appKey,
        account,
        token,
        chatroomId,
        chatroomAddresses,
        chatroomNick: 'none',
        onconnect: function (obj) {
          resove(myChatroom);
        },
        onwillreconnect: function (obj) {
        },
        ondisconnect: function (error) {
        },
        onerror: function (error, obj) {
        },
        onmsgs: function (msgs) {
          for (const msg of msgs) {
            onChatroomMsgs(false, msg);
          }
        },
      });
    });
  };
  return initChatRoom();
}
